<?php
ob_start();
header("content-type:text/html; charset=utf-8");
session_start();
require_once 'inc/config.php';
require_once 'inc/classes/db.php';
require_once 'inc/classes/template.php';
require_once 'inc/classes/util.php';
require_once 'include/staticdata.php';
require_once 'include/global.func.php';
require_once 'include/pagination.php';
require_once 'include/topwrap.php';

function __sort($data) {
	$_tmp = array();
	foreach($data as $new) {
		if( $new['recommen'] ) {
			$_tmp['recommen'][] = $new;
		} else {
			$_tmp[$new['catid']][] = $new;
			$_tmp['raw'][] = $new;
		}
	}
	return $_tmp;
}

$topconfig = Taoapi_Config::Init();
$topconfig->setCharset('utf-8');

$top = new TopWrap(false,true);

$gTemplate['templateDir'] = 'templates/';
$gTemplate['leftDelimiter'] = '{';
$gTemplate['rightDelimiter'] = '}';
$tpl = new Template( $gTemplate );
$tpl->register_function('print_img', 'print_img');
$tpl->register_function('spk_substr', 'spk_substr');
$db  = new db( $gDb );

$PAGESIZE = 20;
util::gpc( );
$tpl->assign('news_static',true);
$tpl->assign('topnav', 	StaticData::getMenuData());
$tpl->assign('smallnav',StaticData::getSmallNav());
$tpl->assign('sort', 	StaticData::getSort());
$tpl->assign('lang',	include('include/lang.php'));

$category = !empty($_GET['catid']) ? intval($_GET['catid']) : 1;

$nav = nav($category);
$tpl->assign('nav',$nav);

$page = !empty($_GET['p']) ? intval($_GET['p']) : 1;
$tpl->assign('page', $page );

$tpl->assign('catid', $category);
$start = ($page - 1) * $PAGESIZE;

$cateinfo = $db->fetchrow("select * from ".$gDb['prefix']."categories where f_id=$category");

$tpl->assign('cateinfo',$cateinfo);

$items = $top->searchItems('0', $cateinfo['f_name'], 1, 40);

$tpl->assign('items', $items['taobaoke_items']['taobaoke_item']);

$subcats = $db->fetchrows("select * from ".$gDb['prefix']."categories where f_parentId=$category");
$tpl->assign('subcats',$subcats);

if($cateinfo['f_haveChild']) {
	$count = $db->fetchrow("select count(*) as count from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentid=$category)");
	$pagination = new Pagination($count['count'], $PAGESIZE);
	$news = $db->fetchrows("select a.f_name as catename,b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentid=$category) order by b.id desc limit $start, $PAGESIZE ");
	$tpl->assign('data', $news);
	$tpl->assign('pagination', $pagination->getPagination());
} else {
	$count = $db->fetchrow('select count(*) as count from '.$gDb['prefix'].'news b left join '.$gDb['prefix']."categories a on b.catid=a.f_id where b.catid='$category'");
	$news = $db->fetchrows('select a.f_name as catename, b.* from '.$gDb['prefix'].'news b left join '.$gDb['prefix']."categories a on b.catid=a.f_id where b.catid='$category' limit $start,$PAGESIZE ");
	$pagination = new Pagination($count['count'],$PAGESIZE);
	$tpl->assign('data', $news);
	$tpl->assign('pagination', $pagination->getPagination());	
}
/*调取右面的数据**/
$news = $db->fetchrows('select a.f_name as catename, b.* from '.$gDb['prefix'].'news b left join '.$gDb['prefix']."categories a on b.catid=a.f_id where b.catid='$category' order by click desc limit 0,12 ");
$tpl->assign('hot', __sort($news));

/*$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=1) or b.catid=1 limit 0, 20";
$emotion = $db->fetchrows($sql); //情感
$tpl->assign('emotion', __sort($emotion));*/

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=11) or b.catid=11 limit 0, 20";
$fashion = $db->fetchrows($sql); //服饰
$tpl->assign('fashion', $fashion);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=20) or b.catid=20 limit 0, 20";
$beauty  = $db->fetchrows($sql); //美容
$tpl->assign('beauty', $beauty);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=32) or b.catid=32 limit 0, 20";
$health  = $db->fetchrows($sql);//健康
$tpl->assign('health', $health);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=43) or b.catid=43 limit 0, 20";
$baby  = $db->fetchrows($sql); //亲子
$tpl->assign('baby', $baby);

$news = $db->fetchrows('select a.f_name as catename, b.* from '.$gDb['prefix'].'news b left join '.$gDb['prefix']."categories a on b.catid=a.f_id where b.catid='$category' order by click desc limit 0,4 ");
$tpl->display('news_list.tpl');
?>